﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Handsfree
{
    public partial class Add_New : Form
    {
        SpeechRecognition s;
        Preferences pref;
        public Add_New()
        {
            pref = new Preferences();
            InitializeComponent();
            hideAll();
            s = new SpeechRecognition(false);
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (KeysetTextbox.Focused)
            {
                if (keyData == Keys.Tab || keyData == Keys.PrintScreen)
                {
                    KeyEventArgs keypress = new KeyEventArgs(keyData);
                    textBox1_KeyDown(new Object(), keypress);
                    return true;
                }
            }
            if (keyTextBox.Focused)
            {
                if (keyData == Keys.Tab || keyData == Keys.PrintScreen)
                {
                    KeyEventArgs keypress = new KeyEventArgs(keyData);
                    keyTextbox_KeyDown(new Object(), keypress);
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void hideAll()
        {
            KSGroupBox.Visible = false;
            KeysetTextbox.Text = "";
            AppGroupBox.Visible = false;
            ApptextBox.Text = "";
            keyGroupBox.Visible = false;
            keyTextBox.Text = "";
            RunGroupBox.Visible = false;
            runTextBox.Text = "";
            submit.Enabled = false;
            Speech.Text = "";
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            String newtext = e.KeyValue.ToString("X");
            if (KeysetTextbox.Text != "")
                newtext = '+' + newtext;
            KeysetTextbox.Text += newtext;
            e.SuppressKeyPress = true;
        }
        private void keyTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            String newtext = e.KeyValue.ToString("X");
            keyTextBox.Text = newtext;
            e.SuppressKeyPress = true;
        }
        private void submit_Click(object sender, EventArgs e)
        {
            string line="";
            switch(comboBox1.SelectedIndex)
            {
                case 0:
                    if (!s.RecognisedApplication(ApptextBox.Text))
                        return;
                    line = "Application/" + Speech.Text + "/" + ApptextBox.Text;
                    break;
                case 1:
                    line = "KeySet/" + Speech.Text + "/" + KeysetTextbox.Text;
                    break;
                case 2:
                    line = "Keyboard/" + Speech.Text + "/" + keyTextBox.Text;
                    break;
                case 3:
                    if (!s.RecognisedRun(runTextBox.Text))
                        return;
                    line = "Run/" + Speech.Text + "/" + runTextBox.Text;
                    break;
            }
            System.IO.StreamWriter file = new System.IO.StreamWriter(pref.getPath(), true);
            file.Write("\r\n"+line);
            file.Close();
            hideAll();
            showGroup();
            s.ReloadGrammar();
            label6.Text = "Command " + Speech.Text + " successfully added!";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            hideAll();
            showGroup();
        }
        private void showGroup()
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    AppGroupBox.Visible = true;
                    break;
                case 1:
                    KSGroupBox.Visible = true;
                    break;
                case 2:
                    keyGroupBox.Visible = true;
                    break;
                case 3:
                    RunGroupBox.Visible = true;
                    break;
            }
        }

        private void OpenButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            ApptextBox.Text = openFileDialog1.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }
        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {
            runTextBox.Text = openFileDialog2.FileName;
        }

        private bool validateCommand(String str)
        {
            List<Command> list = s.returnCommands();
            foreach (Command cm in list)
                if (cm.speech.ToLower() == str.ToLower())
                    return false;
            return true;
        }

        private void Speech_Leave(object sender, EventArgs e)
        {
            if (validateCommand(Speech.Text) && Speech.Text!="")
            {
                label1.Text = "Good to go";
                submit.Enabled = true;
            }
            else
            {
                label1.Text = "Command already exists or Empty Command.";
                submit.Enabled = false;
            }
        }
    }
}
